package org.aspire.graphics.frame;

import org.lwjgl.opengl.GL11;

/******************************************************************************
 * A texture to be bound within OpenGL. This object is responsible for keeping
 * track of a given OpenGL texture and for calculating the texturing mapping
 * coordinates of the full image.
 * <p>
 * Since textures need to be powers of 2 the actual texture may be considerably
 * big that the source image and hence the texture mapping coordinates need to
 * be adjusted to match-up drawing the sprite against the texture.
 *
 * @author Kevin Glass
 * @author Brian Matzon
 *****************************************************************************/
public class Texture {

	// ========================================================================
	// Constants
	// ========================================================================

	// ========================================================================
	// Static Methods
	// ========================================================================

	// ========================================================================
	// Fields
	// ========================================================================

	/**************************************************************************
	 * The GL target type.
	 *************************************************************************/
	private int target;
	
	/**************************************************************************
	 * The GL texture ID.
	 *************************************************************************/
	private int textureID;
	
	/**************************************************************************
	 * The width of the image.
	 *************************************************************************/
	private int width;
	
	/**************************************************************************
	 * The height of the image.
	 *************************************************************************/
	private int height;
	
	/**************************************************************************
	 * The width of the texture.
	 *************************************************************************/
	private int texWidth;
	
	/**************************************************************************
	 * The height of the texture.
	 *************************************************************************/
	private int texHeight;
	
	/**************************************************************************
	 * The ratio of the image width and the texture width.
	 *************************************************************************/
	private float widthRatio;
	
	/**************************************************************************
	 * The ratio of the image height and the texture height.
	 *************************************************************************/
	private float heightRatio;
	
	// ========================================================================
	// Constructors
	// ========================================================================

	/**************************************************************************
	 * Creates a new texture.
	 *
	 * @param target 	the GL target
	 * @param textureID the GL texture ID
	 *************************************************************************/
	public Texture(int target, int textureID) {
		this.target = target;
		this.textureID = textureID;
	}
	
	// ========================================================================
	// Getters & Setters
	// ========================================================================

	/**************************************************************************
	 * Returns the height of the original image.
	 *************************************************************************/
	public int getImageHeight() {
		return this.height;
	}

	/**************************************************************************
	 * Returns the width of the original image.
	 *************************************************************************/
	public int getImageWidth() {
		return this.width;
	}

	/**************************************************************************
	 * Returns the width of the texture.
	 *************************************************************************/
	public int getTextureWidth() {
		return this.texWidth;
	}

	/**************************************************************************
	 * Returns the height of the texture.
	 *************************************************************************/
	public int getTextureHeight() {
		return this.texHeight;
	}


	/**************************************************************************
	 * Returns the height of the physical texture.
	 *************************************************************************/
	public float getHeight() {
		return this.heightRatio;
	}

	/**************************************************************************
	 * Returns the width of the physical texture.
	 *************************************************************************/
	public float getWidth() {
		return this.widthRatio;
	}
	
	/**************************************************************************
	 * Sets the width of the image.
	 *
	 * @param width 	the width of the image
	 *************************************************************************/
	public void setWidth(int width) {
		this.width = width;
		this.setWidth();
	}

	/**************************************************************************
	 * Sets the height of the image.
	 *
	 * @param height 	the height of the image
	 *************************************************************************/
	public void setHeight(int height) {
		this.height = height;
		this.setHeight();
	}

	/**************************************************************************
	 * Set the height of this texture
	 *
	 * @param texHeight The height of the texture
	 *************************************************************************/
	public void setTextureHeight(int texHeight) {
		this.texHeight = texHeight;
		this.setHeight();
	}

	/**************************************************************************
	 * Set the width of this texture
	 *
	 * @param texWidth The width of the texture
	 *************************************************************************/
	public void setTextureWidth(int texWidth) {
		this.texWidth = texWidth;
		this.setWidth();
	}

	/**************************************************************************
	 * Set the width of the texture. This will update the
	 * ratio also.
	 *************************************************************************/
	private void setWidth() {
		if (this.texWidth != 0) {
			this.widthRatio = ((float) this.width) / this.texWidth;
		}
	}
	
	/**************************************************************************
	 * Set the height of the texture. This will update the ratio also.
	 *************************************************************************/
	private void setHeight() {
		if (this.texHeight != 0) {
			this.heightRatio = ((float) this.height) / this.texHeight;
		}
	}
	
	// ========================================================================
	// Override Methods
	// ========================================================================

	// ========================================================================
	// Methods
	// ========================================================================

	/**************************************************************************
	 * Bind the specified GL context to a texture.
	 *
	 * @param gl 	the GL context to bind to
	 *************************************************************************/
	public void bind() {
		GL11.glBindTexture(this.target, this.textureID);
	}
	// ========================================================================
	// Inner & Anonymous Classes
	// ========================================================================
}
